package cn.lxg.service.impl;

import cn.lxg.dao.ICommentDao;
import cn.lxg.dao.INovelChapterDao;
import cn.lxg.dao.INovelDao;
import cn.lxg.pojo.TComment;
import cn.lxg.pojo.TNovel;
import cn.lxg.pojo.TNovelChapter;
import cn.lxg.service.ICommentService;
import cn.lxg.vo.CommentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements ICommentService {

    @Autowired
    private final ICommentDao commentDao = null;

    @Override
    @Transactional
    public List<CommentVO> getList(Integer limit, Integer page, CommentVO comment) {
        /* 计算页面 */
        Integer limitStart = null;
        Integer limitEnd = null;
        if (limit != null && page != null) {
            limitStart = (page - 1) * limit;
            limitEnd = limit;
        }
        return this.commentDao.select(limitStart, limitEnd, comment);
    }

    @Override
    public int countDataLine(CommentVO comment) {
        return 0;
    }

    @Autowired
    INovelDao novelDao;

    @Autowired
    ICommentDao commentDao1;

    @Override
    public String add(CommentVO commentVO) {
        TComment comment = new TComment();
        comment.setContent(commentVO.getContent());
        comment.setCreateTime(new Date());

        TNovel novel = new TNovel();
        novel.setName(commentVO.getNovelName());
        novel = novelDao.selectOne(novel);
        comment.setNovelId(novel.getId());

        int row = commentDao1.add1(comment);
        if (row==1){
            return "添加成功";
        }else {
            return "添加失败";
        }
    }

    @Override
    public CommentVO get(Integer id) {
        return commentDao.get(id);
    }

    @Override
    public String delete(Integer id) {
        int row = commentDao.delete(id);
        if (row == 1) {
            return "删除成功";
        } else {
            return "删除失败";
        }
    }

    @Override
    public String edit(TComment comment) {
        int row = commentDao.edit(comment);
        if (row == 1) {
            return "编辑成功";
        } else {
            return "编辑失败";
        }
    }
}
